package com.foryou.wechat.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.foryou.wechat.common.enums.ImageTypeEnum;
import com.foryou.wechat.entity.UserImageEntity;
import com.foryou.wechat.mapper.UserImageMapper;
import com.foryou.wechat.model.vo.FileVo;
import com.foryou.wechat.service.UserImageService;
import com.foryou.wechat.utils.FileUtil;
import com.foryou.wechat.utils.HttpContextUtils;
import com.foryou.wechat.utils.Response;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;

/**
 * Created by Lee.
 */
@Service
public class UserImageServiceImpl extends ServiceImpl<UserImageMapper,UserImageEntity>
        implements UserImageService {

    @Value("${web.upload}")
    private String uploadPrefix;

    @Value("${web.url}")
    private String url;

    @Override
    @Transactional
    public boolean userUploadFiles(Long businessId,MultipartFile[] files, ImageTypeEnum imageTypeEnum)
            throws Exception{

        if (null == files || files.length == 0) {
            return false;
        }

        // 从session 获取用户ID TODO
        Long sessionUserId = HttpContextUtils.getUserId();

        List<UserImageEntity> userImageEntityList = new ArrayList<>();
        for (MultipartFile file : files) {
            FileVo fileVo = FileUtil.uploadFile(uploadPrefix, file);
            String dataUrl = url.concat(fileVo.getFileName());
            userImageEntityList.add(new UserImageEntity(dataUrl,businessId,
                    imageTypeEnum.getCode(),fileVo.getSize(),sessionUserId));
        }

        //执行批量上传
        return super.saveBatch(userImageEntityList);
    }

    @Override
    public List<String> imageUrlList(Long businessId, ImageTypeEnum imageTypeEnum) {
        List<UserImageEntity> list = super.list(new QueryWrapper<UserImageEntity>()
                .eq("business_id", businessId)
                .eq("image_type", imageTypeEnum.getCode())
        );
        return list.stream().map(UserImageEntity::getImageUrl).collect(Collectors.toList());
    }

    @Override
    public Response uploadForWorks(Long businessId, MultipartFile[] files) throws Exception {
        userUploadFiles(businessId, files, ImageTypeEnum.WORKS);
        return Response.success();
    }

    @Override
    public Response uploadForShoot(Long businessId, MultipartFile[] files) throws Exception {
        userUploadFiles(businessId, files, ImageTypeEnum.SHOOT);
        return Response.success();
    }

    @Override
    public Response uploadForFiled(Long businessId, MultipartFile[] files) throws Exception {
        userUploadFiles(businessId, files, ImageTypeEnum.FILED);
        return Response.success();
    }
}
